package com.ebid.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class LoopJson {


    private static void createXML(String str){
        JSONObject jsonObj = JSON.parseObject(str);

        LinkedHashMap<String, String> jsonMap = JSON.parseObject(str, new TypeReference<LinkedHashMap<String, String>>() {
        });

        for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        //1.创建document对象，代表整个xml文档
        Document document = DocumentHelper.createDocument();
        //2.创建根节点rss
        //3.向rss节点中添加version属性
        //4.生成子节点及节点内容
        //5.设置生成xml的格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");

        Element rootElement = document.addElement("test");

        for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
            if (entry.getValue() instanceof String){
                System.out.println(entry.getKey() + ":" + entry.getValue());
                Element element = rootElement.addElement(entry.getKey());
                element.setText(entry.getValue());
            }



        }

        //6.生成xml文件
        File file = new File("test.xml");
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileOutputStream(file), format);
            //设置是否转义，默认值是true，代表转义
            writer.setEscapeText(false);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void loopJson(String str){
        JSONObject jsonObj = JSON.parseObject(str);

        LinkedHashMap<String, String> jsonMap = JSON.parseObject(str, new TypeReference<LinkedHashMap<String, String>>() {
        });

        for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }

    public static void main(String[] args) {

        Document documentNew = DocumentHelper.createDocument();
        documentNew.setXMLEncoding("UTF-8");

        Element rootElement = documentNew.addElement("qualifyBulletinPhase");

//        String s = "{'A':{'a':'1' ,'aa':'11'},'B':{'b':'2' ,'bb':'22'}}" ;

        String ss = "{'A':'ASA','B':'BSB'}";

//        String sss = "'A':'AAA','B':'BBB'},'C':{'C1','C2','C3'}";
        createXML(ss);

//        loopJson(ss);

        //5.设置生成xml的格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        //6.生成xml文件
        File file = new File("qualifyBulletinPhase.xml");
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileOutputStream(file), format);
            //设置是否转义，默认值是true，代表转义
            writer.setEscapeText(false);
            writer.write(documentNew);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
